package edu.xhu.communityantiepidemicsystemback.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class RequestHeaderAuth {

    public static boolean checkRequestHeader(){
        //获取请求的属性
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if(Objects.isNull(requestAttributes)){
            return false;
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        //获取当前请求
        HttpServletRequest httpServletRequest=servletRequestAttributes.getRequest();
        //获取请求头信息的值
        String key = httpServletRequest.getHeader("x-auth-code");
        //如果为空返回false,然后对比服务端生成的签名，相同则通过，不同则返回false
        if(StringUtils.isEmpty(key)){
            return false;
        }
        if(key.equals(Md5Utils.md5())){
            return true;
        }
        return false;
    }

}
